#include <stdio.h>
#include <string.h>

// RS232C编码函数
int rs232c_encode(double *volts, int volts_size, const char *msg, int size) {
    if (size != 1 || volts_size != 9) {
        return -1;
    }
    // 空闲位，逻辑1，对应负电压
    volts[0] = -10.0; 
    // 起始位，逻辑0，对应正电压
    volts[1] = 10.0; 

    char ch = msg[0];
    // 取7位ASCII码，位小端序
    for (int i = 0; i < 7; i++) {
        volts[i + 2] = (ch & (1 << i))? -10.0 : 10.0; 
    }
    // 终止位，逻辑1，对应负电压
    volts[9 - 1] = -10.0; 

    return 0;
}

// RS232C解码函数，题目要求只实现编码，但这里也给出解码示例
int rs232c_decode(char *msg, int size, const double *volts, int volts_size) {
    if (size != 1 || volts_size != 9) {
        return -1;
    }
    char ch = 0;
    // 跳过起始位
    for (int i = 2; i < 9 - 1; i++) {
        ch |= (volts[i] < 0) << (i - 2); 
    }
    msg[0] = ch;
    return 0;
}

int main() {
    double volts[9];
    const char input_char[] = "A";
    int result = rs232c_encode(volts, sizeof(volts)/sizeof(volts[0]), input_char, strlen(input_char));

    if (result == 0) {
        printf("RS232C encoding result:\n");
        for (int i = 0; i < sizeof(volts)/sizeof(volts[0]); i++) {
            printf("Voltage at position %d: %.1f\n", i, volts[i]);
        }
    } else {
        printf("Encoding failed.\n");
    }

    return 0;
}